package com.qfedu.yige.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.yige.common.Result;
import com.qfedu.yige.common.Utils;
import com.qfedu.yige.mapper.BatterySwappingCabinetMapper;
import com.qfedu.yige.mapper.CollectMapper;
import com.qfedu.yige.mapper.UserMapper;
import com.qfedu.yige.pojo.BatterySwappingCabinet;
import com.qfedu.yige.pojo.Collect;
import com.qfedu.yige.pojo.PowerUser;
import com.qfedu.yige.service.BatterySwappingCabinetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class BatterySwappingCabinetServiceImpl extends ServiceImpl<BatterySwappingCabinetMapper,BatterySwappingCabinet> implements BatterySwappingCabinetService{


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CollectMapper collectMapper;

    @Override
    public List<BatterySwappingCabinet> Nearby(double x, double y) {
        List<BatterySwappingCabinet> list = list();
        List<BatterySwappingCabinet> nlist = new ArrayList<BatterySwappingCabinet>();
        for (BatterySwappingCabinet battery : list) {
            double xx = Double.parseDouble(battery.getLongitude());
            double yy = Double.parseDouble(battery.getDimension());
            double distance = Utils.getDistance(x,y,xx,yy);
            if (distance<Utils.getNEAR_DISTANCE()){
                nlist.add(battery);
            }
        }
        return nlist;
    }

    @Override
    public Result subscribe(int bscId, int userid) {
        PowerUser user = userMapper.selectById(userid);
        if (user.getSubscribeBSCId() == 0) {
            user.setSubscribeBSCId(bscId);
            userMapper.updateById(user);
            baseMapper.subscribe(bscId);
            return Result.success("");
        }else if (user.getSubscribeBSCId() ==bscId ) {
            return Result.fail("已预约此充电桩");
        }
        return Result.fail("预约失败");

    }

    @Override
    public Result cancelSubscribe(int bscId, int userid) {
        PowerUser user = userMapper.selectById(userid);
        if (user.getSubscribeBSCId() == bscId) {
            user.setSubscribeBSCId(0);
            userMapper.updateById(user);
            baseMapper.cancelSubscribe(bscId);
            return Result.success("");

        }else if (user.getSubscribeBSCId() ==0 ) {
            return Result.fail("未预约此充电桩");
        }
        return Result.fail("预约失败");
    }

    @Override
    public Result collect(int bscId, int userid) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<Collect>();
        queryWrapper.eq("BSC_ID", bscId).and(i -> i.eq("uid", userid));
        Collect collect = collectMapper.selectOne(queryWrapper);
        if(collect == null) {
            int row = collectMapper.insert(new Collect(bscId,userid));
            if(row>0) {
                return Result.success("");
            }
            return Result.fail("收藏失败");
        }else {
            return Result.fail("已收藏，重复收藏无效");
        }

    }

    @Override
    public Result cancelCollect(int bscId, int userid) {

        QueryWrapper<Collect> queryWrapper = new QueryWrapper<Collect>();
        queryWrapper.eq("BSC_ID", bscId).and(i -> i.eq("uid", userid));
        Collect collect = collectMapper.selectOne(queryWrapper);
        if(collect != null) {
            int row = collectMapper.delete(queryWrapper);
            if(row>0) {
                return Result.success("");
            }
            return Result.fail("取消收藏失败");
        }else {
            return Result.fail("未收藏");
        }
    }

    @Override
    public Result collectList(int userid) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<Collect>();
        queryWrapper.eq("uid", userid);
        List<Collect> list = collectMapper.selectList(queryWrapper);
        return Result.success(list);
    }


}
